package com.example.exception;

import com.example.response.AjaxJson;
import org.mybatis.spring.MyBatisSystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;

import static com.example.utils.ResultCodeUtil.*;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.sql.SQLSyntaxErrorException;
import java.sql.SQLTransientConnectionException;


@ResponseBody
@ControllerAdvice
public class GlobalException {
    private static final Logger log = LoggerFactory.getLogger(GlobalException.class);

    @ExceptionHandler(value = ServiceException.class)
    public AjaxJson serviceException(ServiceException e) {
        return AjaxJson.error(e.getCode(), e.getMessage(), e.getDescription());
    }
    @ExceptionHandler(NoHandlerFoundException.class)
    public AjaxJson noHandlerFoundException(NoHandlerFoundException e) {
        return AjaxJson.error(NOT_PATH);
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public AjaxJson httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return AjaxJson.error(METHOD_ERROR, "不支持的请求方式:" + e.getMethod());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public AjaxJson httpMessageNotReadableException(HttpMessageNotReadableException e) {
        return AjaxJson.error(NULL_ERROR, "请求数据错误");
    }
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public AjaxJson sqlSyntaxErrorException(SQLSyntaxErrorException e) {
        log.error(e.getMessage());
        return AjaxJson.error(DATABASE_ERROR,"sql异常");
    }

    @ExceptionHandler({SocketTimeoutException.class, ConnectException.class})
    public AjaxJson socketTimeoutException(Exception e) {
        return AjaxJson.error(CONNECT_OUT);
    }
    @ExceptionHandler({SQLTransientConnectionException.class, CannotGetJdbcConnectionException.class, MyBatisSystemException.class})
    public AjaxJson sqlTransientConnectionException(Exception e) {
        return AjaxJson.error(CONNECT_OUT,"数据库连接不可用");
    }
    @ExceptionHandler({JedisConnectionException.class, JedisDataException.class, RedisConnectionFailureException.class})
    public AjaxJson jedisConnectionException(Exception e) {
        return AjaxJson.error(DATABASE_ERROR,"Redis连接异常");
    }
    @ExceptionHandler(NullPointerException.class)
    public AjaxJson nullPointerException(NullPointerException e) {
        return AjaxJson.error(SYSTEM_ERROR,"空指针异常");
    }

    @ExceptionHandler(value = Exception.class)
    public AjaxJson Exception(Exception e) {
        log.error(e.getMessage());
        return AjaxJson.error(SYSTEM_ERROR);
    }
}
